package com.atguigu.yygh.host.testmongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author DongYu Zhou
 * @date 2022/6/22 22:31
 */
@RestController
@RequestMapping("/mongo2")
public class TestMongo2 {

    @Autowired
    private UserRepository userRepository;

    //添加
    @RequestMapping("/add")
    public User addUser(){
        User user = new User();
        user.setAge(225);
        user.setName("test666");
        user.setEmail("4932200@qq.com");
        User user1 = userRepository.insert(user);
        System.out.println("user1 = " + user1);
        return user1;
    }

    //查询所有
    @RequestMapping("/getAll")
    public List<User> getAll(){
        List<User> userList = userRepository.findAll();
        userList.forEach(System.out::println);
        return userList;
    }

    //根据id查询
    @RequestMapping("/getById")
    public Optional<User> getById(){
        Optional<User> byId = userRepository.findById("62b3288dc039993e2e2d612a");
        System.out.println("byId = " + byId);
        return byId;
    }

    //条件查询
    @RequestMapping("/findUSer")
    public void findUSer(){
        User user = new User();
        user.setAge(225);
        user.setName("test666");
        Example<User> example = Example.of(user);
        List<User> userList = userRepository.findAll(example);
        for (User user1 : userList) {
            System.out.println("user1 = " + user1);
        }
    }


    //条件模糊查询
    @GetMapping("findUserLike")
    public void findUserLike(){
        User user = new User();
        user.setName("三");

        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配规则，改成默认查询
                .withIgnoreCase(true);

        Example<User> example = Example.of(user,matcher);
        List<User> users = userRepository.findAll(example);
        users.forEach(System.out::println);
    }

    //条件模糊查询
    @GetMapping("findUserPage")
    public void findUserPage(){
        Sort sort = Sort.by(Sort.Direction.DESC,"age");
        //第一页为0
        Pageable pageable = PageRequest.of(0,10,sort);

        User user = new User();
        user.setName("三");
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配规则，改成默认查询
                .withIgnoreCase(true);

        Example<User> example = Example.of(user,matcher);
        Page<User> page = userRepository.findAll(example,pageable);
        System.out.println("page = " + page);
    }


    //修改
    @GetMapping("update")
    public void updateUser() {
        User user = userRepository.findById("62b2c5fe031036091d6122c7").get();
        user.setName("张三_1");
        User user1 = userRepository.save(user);
        System.out.println("user1 = " + user1);
    }

    //删除操作
    @GetMapping("delete")
    public void delete() {
        userRepository.deleteById("62b2c5fe031036091d6122c7");
    }

    @GetMapping("testMethod")
    public void testMethod() {
        List<User> users =  userRepository.getByNameAndAge("张三",20);
        users.forEach(System.out::println);
    }

    @GetMapping("testMethod2")
    public void testMethod2() {
        List<User> users =  userRepository.getByNameLike("三");
        users.forEach(System.out::println);
    }


}
